package com.it.emall.mapper;

import com.it.emall.domain.Recipes;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 用户收藏Mapper
 */
@Mapper
public interface UserFavoritesMapper {


    /**
     * 检查用户是否已经收藏某个食谱
     */
    @Select("SELECT COUNT(*) FROM user_favorites WHERE user_id = #{userId} AND recipe_id = #{recipeId}")
    int isUserFavoritedRecipe(@Param("userId") int userId, @Param("recipeId") int recipeId);

    /**
     * 添加收藏记录
     */
    @Insert("INSERT INTO user_favorites (user_id, recipe_id, created_at) VALUES (#{userId}, #{recipeId}, NOW())")
    int addFavorite(@Param("userId") int userId, @Param("recipeId") int recipeId);

    /**
     * 取消收藏记录
     */
    @Delete("DELETE FROM user_favorites WHERE user_id = #{userId} AND recipe_id = #{recipeId}")
    int removeFavorite(@Param("userId") int userId, @Param("recipeId") int recipeId);

    /**
     * 根据用户ID查询所有收藏的食谱
     */
    @Select("SELECT r.* FROM recipes r " +
            "INNER JOIN user_favorites uf ON r.id = uf.recipe_id " +
            "WHERE uf.user_id = #{userId}")
    List<Recipes> findFavoritesByUserId(int userId);

}
